<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('已完成-租借记录列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <input type="hidden" th:value="${isBack}" name="isBack"/>
                        <li>
                            <label>订单渠道：</label>
                            <select name="channel" th:with="type=${@dict.getType('sys_used_channel')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>租借类型：</label>
                            <select name="borrowMode" th:with="type=${@dict.getType('sys_used_borrow_mode')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>订单状态：</label>
                            <select name="status" th:with="type=${@dict.getType('sys_used_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <!--<li>-->
                        <!--<label>是否归还：</label>-->
                        <!--<select name="isBack" th:with="type=${@dict.getType('sys_used_is_back')}">-->
                        <!--<option value="">所有</option>-->
                        <!--<option th:each="dict : ${type}" th:text="${dict.dictLabel}"-->
                        <!--th:value="${dict.dictValue}"></option>-->
                        <!--</select>-->
                        <!--</li>-->
                        <li>
                            <label>是否VIP：</label>
                            <select name="isVip" th:with="type=${@dict.getType('sys_used_is_back')}">
                                <option value="">所有</option>
                                <option value="1">是</option>
                                <option value="0">否</option>
                            </select>
                        </li>
                        <li>
                            <label style="width: 105px;">关键字查询字段：</label>
                            <select name="fields">
                                <option value="orderNumber" selected>订单号</option>
                                <option value="phone">授权手机号</option>
                                <option value="appUserId">用户ID</option>
                                <option value="borrowSn">借出机柜SN</option>
                                <option value="batterySn">借出电池SN</option>
                                <option value="partnerFullName">直属合作商</option>
                                <option value="merchantFullName">直属商户</option>
                                <option value="storeName">直属门店</option>
                            </select>
                            <input type="text" name="keywords"/>
                        </li>
                        <input type="hidden" name="isM" value="0"/>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <!--<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:used:add">-->
            <!--<i class="fa fa-plus"></i> 添加-->
            <!--</a>-->
            <!--<a class="btn btn-primary single disabled" onclick="$.operate.edit()"-->
            <!--shiro:hasPermission="system:used:edit">-->
            <!--<i class="fa fa-edit"></i> 修改-->
            <!--</a>-->
            <!--<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"-->
            <!--shiro:hasPermission="system:used:remove">-->
            <!--<i class="fa fa-remove"></i> 删除-->
            <!--</a>-->
            <!--<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:used:export">-->
            <!--<i class="fa fa-download"></i> 导出-->
            <!--</a>-->
            <a class="btn btn-success" onclick="$.operate.toolkeys()">
                <i class="fa fa-eye"></i> 一键展开
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:used:edit')}]];

    var endUsedFlag = [[${@permission.hasPermi('system:used:endUsed')}]];

    var removeFlag = [[${@permission.hasPermi('system:used:remove')}]];
    var channelDatas = [[${@dict.getType('sys_used_channel')}]];
    var borrowModeDatas = [[${@dict.getType('sys_used_borrow_mode')}]];
    var isBackDatas = [[${@dict.getType('sys_used_is_back')}]];
    var statusDatas = [[${@dict.getType('sys_used_status')}]];


    var devTypeDatas = [[${@dict.getType('sys_device_type')}]];

    var prefix = ctx + "system/used";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "租借订单",
            detailView: true,
            rowStyle: function (row, index) {   // 动态修改行的颜色
                return {css: {"background-color": '#99e8e8'}};
            },
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.leaseState == 1 && row.isBack == 0) {
                            // 租借中的订单可以结束订单
                            // 未归还，租借成功，可出现结束订单按钮，结束订单
                            actions.push('<a class="btn btn-info btn-xs ' + endUsedFlag + '" href="javascript:void(0)" onclick="endUsed(\'' + row.id + '\')"><i class="fa fa-database"></i>结束订单</a> ');
                        }
                        actions.push('  <a class="btn btn-primary btn-xs ' + endUsedFlag + '" href="javascript:void(0)" onclick="seeDetails(\'' + row.id + '\')">查看详情</a> ');

                        return actions.join('');
                    }
                },
                {
                    field: 'orderNumber',
                    title: '订单号'
                },
                {
                    field: 'appUserId',
                    title: '用户昵称(VIP)(用户ID)(手机号)',
                    formatter: function (value, row, index) {
                        var imagesUrl = "";
                        if (row.avatar != null && row.avatar != "") {
                            imagesUrl = $.table.imageView(row.avatar);
                        }
                        var nickName = row.nickName;
                        if (nickName == "" || nickName == null) {
                            nickName = "-";
                        }
                        var mobile = row.mobile;
                        if (mobile == "" || mobile == null) {
                            mobile = "-";
                        }
                        var mobile = "<span style='color: orangered'>(" + mobile + ")</span>";
                        var isVip = "<span style='color: orange;'>否</span>";
                        if (row.isFree == 1) {
                            isVip = "<span style='color: green;'>是</span>";
                        }
                        return imagesUrl + "&nbsp;" + nickName + "(" + isVip + ")&nbsp;(" + row.appUserId + ")&nbsp;" + mobile;
                    }
                },
                {
                    field: 'channel',
                    title: '订单渠道',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(channelDatas, value);
                    }
                },
                {
                    field: 'borrowMode',
                    title: '租借类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(borrowModeDatas, value);
                    }
                },
                {
                    field: 'storeName',
                    title: '门店名'
                },
                {
                    field: 'rentDeviceSn',
                    title: '借出机柜SN(设备类型)',
                    formatter: function (value, row, index) {
                        return row.rentDeviceSn + "~(" + $.table.selectDictLabel(devTypeDatas, row.devType) + ")";
                    }
                },
                {
                    field: 'batterySn',
                    title: '电池SN'
                },
                {
                    field: 'leaseState',
                    title: '租借状态(借出时间)',
                    formatter: function (value, row, index) {
                        // 租借状态 1 成功 2 失败 0 初始
                        var leaseStateMsg = "-";
                        if (row.leaseState == 1) {
                            leaseStateMsg = "<span style='color: black'>成功</span>" + "&nbsp;<span style='color: black;'>-(" + row.rentDate + ")</span>";
                        } else if (row.leaseState == 2) {
                            leaseStateMsg = "<span style='color: red'>失败</span>";
                        } else if (row.leaseState == 0) {
                            leaseStateMsg = "<span>初始</span>";
                        }
                        return leaseStateMsg;
                    }
                },
                {
                    field: 'isBack',
                    title: '是否归还(归还时间)',
                    formatter: function (value, row, index) {
                        var isBackMsg = "";
                        // 是否归还 0 未归还 1 归还
                        if (row.isBack == 1) {
                            isBackMsg = "<span style='color: black;'>已归还</span>" + "&nbsp;<span style='color: black;'>-(" + row.backDate + ")</span>";
                        } else {
                            isBackMsg = "<span style='color: red;'>未归还</span>";
                        }
                        return isBackMsg
                    }
                },
                {
                    field: 'usingTime',
                    title: '使用时间',
                    formatter: function (value, row, index) {
                        return "<span style='color: black;'>" + timeStamp(value) + "</span>";
                    }
                },
                {
                    field: 'amountPaid',
                    title: '实付金额'
                },
                {
                    field: 'discountAmount',
                    title: '优惠金额'
                },
                {
                    field: 'status',
                    title: '订单状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'refundAmount',
                    title: '退款金额'
                },
            ]
        };
        $.table.init(options);
    });


    function seeDetails(id) {
        var url = prefix + "/seeDetails/" + id;
        var width = null;
        var height = null;
        if ($.common.isMobile()) {
            width = 'auto';
            height = 'auto';
        }
        if ($.common.isEmpty(url)) {
            url = "/404.html";
        }
        if ($.common.isEmpty(width)) {
            width = 1300;
        }
        if ($.common.isEmpty(height)) {
            height = ($(window).height() - 50);
        }
        layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "查看订单详情",
            content: url,
            btn: ['关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            cancel: function (index) {
                return true;
            }
        });
    }


    initChildTable = function (index, row, $detail) {
        // 展示详情
        var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(childTable).bootstrapTable({
            url: prefix + "/list",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            queryParams: {
                id: row.id
            },
            columns: [
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'partnerFullName',
                    title: '直营合作商'
                },
                {
                    field: 'merchantFullName',
                    title: '直营商户'
                },
                // {
                //     field: '',
                //     title: '授权订单号'
                // },
                {
                    field: 'mealContent',
                    title: '套餐描述'
                },
                {
                    field: 'endReason',
                    title: '后台结束扣费描述'
                }
            ]
        });
    };


    function endUsed(id) {
        // 结束租借订单
        var url = prefix + "/endUsed/" + id;
        $.modal.open("结束租借订单", url, 800, 460);
    }


    function timeStamp(second_time) {
        var time = parseInt(second_time) + "秒";
        if (parseInt(second_time) > 60) {
            var second = parseInt(second_time) % 60;
            var min = parseInt(second_time / 60);
            time = min + "分" + second + "秒";
            if (min > 60) {
                min = parseInt(second_time / 60) % 60;
                var hour = parseInt(parseInt(second_time / 60) / 60);
                time = hour + "小时" + min + "分" + second + "秒";
                if (hour > 24) {
                    hour = parseInt(parseInt(second_time / 60) / 60) % 24;
                    var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
                    time = day + "天" + hour + "小时" + min + "分" + second + "秒";
                }
            }
        }
        return time;
    }
</script>
</body>
</html>